package com.example.sunsg.okhttptest.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.Manager.FrescoManager;
import com.example.sunsg.okhttptest.activity.LifeStoryActivity;
import com.example.sunsg.okhttptest.bean.LocalImageItem;
import com.example.sunsg.okhttptest.bean.LocalImageItemImage;
import com.example.sunsg.okhttptest.bean.LocalImageItemTitle;
import com.example.sunsg.okhttptest.util.DisplayUtils;
import com.example.sunsg.okhttptest.viewholder.LocalImageImageHolder;
import com.example.sunsg.okhttptest.viewholder.LocalImageTitleHolder;

import java.util.ArrayList;

/**
 * Created by sunsg on 16/3/2.
 */
public class LocalImageSelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{

    private ArrayList<LocalImageItem> mItems;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    /**图片的宽*/
    private int mImageWidth;
    /**图片之间的距离*/
    public static final int IMAGE_PADDING = 3;
    private LifeStoryImagesAdapter.AdapterCallBack mCallBack;
    private ArrayList<LocalImageItemImage> mCheckedImages;

    public LocalImageSelectAdapter(Context context, LifeStoryImagesAdapter.AdapterCallBack callBack) {
        this.mContext = context;
        mCallBack = callBack;
        mLayoutInflater = LayoutInflater.from(mContext);
        mImageWidth = (int) 1.0 * (DisplayUtils.getScreenWidth(context) / LifeStoryActivity.COLUMNS_COUNT);
        mCheckedImages = new ArrayList<>();
    }

    public void setmItems(ArrayList<LocalImageItem> items){
        mItems = items;
        notifyDataSetChanged();
    }

    public ArrayList<LocalImageItem> getmItems(){
        return mItems;
    }

    public ArrayList<LocalImageItemImage> getChekedImages(){
        return mCheckedImages;
    }

    public void clearCheckeImages(){
        mCheckedImages.clear();
    }

    /**
     * 更新选中图片状态
     * @param position
     */
    public void updateChecked(int position){
        if(mItems != null){
            LocalImageItem item = mItems.get(position);
            if(item.getType() == LocalImageItem.TYPE_IMAGE){
                LocalImageItemImage image = (LocalImageItemImage) item.getChildItem();
                image.setChecked(image.isChecked() ? false :true);
                notifyDataSetChanged();
                if(image.isChecked()){
                    if(!mCheckedImages.contains(image)){
                        image.getImage().setupDimension();
                        mCheckedImages.add(image);
                    }
                }else{
                    if(mCheckedImages.contains(image)){
                        mCheckedImages.remove(image);
                    }
                }
            }
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View itemView = null;
        switch (viewType){
            case LocalImageItem.TYPE_TITLE:
                itemView = mLayoutInflater.inflate(R.layout.item_local_image_select_title,parent,false);
                holder = new LocalImageTitleHolder(itemView);
            break;
            case LocalImageItem.TYPE_IMAGE:
                itemView = mLayoutInflater.inflate(R.layout.item_local_image_select_image,parent,false);
                holder = new LocalImageImageHolder(itemView);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        LocalImageItem item = mItems.get(position);
        int type = item.getType();
        switch (type){
            case LocalImageItem.TYPE_TITLE:
                LocalImageTitleHolder titleHolder = (LocalImageTitleHolder) holder;
                if(item.getChildItem() instanceof LocalImageItemTitle){
                    LocalImageItemTitle titleItem = (LocalImageItemTitle) item.getChildItem();
                    titleHolder.title.setText(titleItem.getTitle());
                }

                break;
            case LocalImageItem.TYPE_IMAGE:
                LocalImageImageHolder imageHolder = (LocalImageImageHolder) holder;
                if(item.getChildItem() instanceof LocalImageItemImage ){
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mImageWidth,mImageWidth);
                    imageHolder.image.setLayoutParams(params);
                    final LocalImageItemImage imageItem = (LocalImageItemImage) item.getChildItem();
                    //图片
                    FrescoManager.loadFilePath(imageItem.getImage().getDataPath()).resize(mImageWidth,mImageWidth).autoRotate(true).builder().into(imageHolder.image);
                    imageHolder.image.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(mContext,"index = "+imageItem.getIndex(),Toast.LENGTH_SHORT).show();
                        }
                    });
                    imageHolder.imSelect.setSelected(imageItem.isChecked());
                    imageHolder.vBg.setVisibility(imageItem.isChecked() ? View.VISIBLE : View.GONE);
                    imageHolder.imSelect.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateChecked(position);
                            if(mCallBack != null){
                                mCallBack.setSelect(mCheckedImages.size());
                            }
                        }
                    });
                }
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mItems == null ? 0 : mItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mItems.get(position).getType();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            /**是否选中图标*/
            case R.id.ls_grid_check:

                break;
//            /**图片*/
//            case R.id.image:
//                break;
        }
    }
}
